import pytest
import json
from unittest.mock import MagicMock
from unittest.mock import patch
from fastapi.testclient import TestClient
from appCustomers.main import app
from appCustomers.utils import customers_utl
from appCustomers.utils.sec_utils import create_standard_token

client = TestClient(app)

cust_id_for_test = "1869eb93-5aba-4e1c-a8cc-a4b7a2f4f136"

@pytest.fixture(scope="module")
def get_test_token():
    test_data={"sub": "mock@mail.me", "phone": "78980987432", "custid": cust_id_for_test }
    auth_data = create_standard_token(test_data)
    return f"Bearer {auth_data["access_token"]}"

async def mock_response(pl: json):
    return pl

@pytest.mark.test_func
def test_read_item_with_mock(get_test_token):
    payload = json.dumps({
        "id": cust_id_for_test
    })
    headers = {
        'Content-Type': 'application/json',
        'Authorization': get_test_token
        #'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqbTIzMzRAYmFuay5jb20iLCJwaG9uZSI6InA5MzY3Nzc1NzY0IiwiY3VzdGlkIjoiZGExNWZkNWEtNWIyNS00ODRiLTk0NjUtMDM3ZDc5NTg2YjA5IiwiZXhwIjoxNzExMjkyODUyfQ.-9z5OZHTA18CdjpGeIKhBHUjIdFwyuVcRrqDYfbIRW0'
    }

    print(f"token={get_test_token}")

    mock_data = {'status': 'MOK'}
    mock_get = MagicMock(return_value=mock_response(mock_data))

    with patch('appCustomers.utils.customers_utl.do_cust_check', mock_get):
        response = client.request("GET", "/customer/check", headers=headers, data=payload)

    print(f"res_code={response.status_code}")
    print(f"raw={response}")

    assert response.status_code == 200
    assert response.json() == mock_data

@pytest.mark.test_func
def test_read_item_without_mock(get_test_token):

    payload = json.dumps({
        "id": cust_id_for_test
    })
    headers = {
        'Content-Type': 'application/json',
        'Authorization': get_test_token
        #'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqbTIzMzRAYmFuay5jb20iLCJwaG9uZSI6InA5MzY3Nzc1NzY0IiwiY3VzdGlkIjoiZGExNWZkNWEtNWIyNS00ODRiLTk0NjUtMDM3ZDc5NTg2YjA5IiwiZXhwIjoxNzExMjkyODUyfQ.-9z5OZHTA18CdjpGeIKhBHUjIdFwyuVcRrqDYfbIRW0'
    }

    response = client.request("GET", "/customer/check", headers=headers, data=payload)

    assert response.status_code == 200
    assert response.json() == {'status': 'OK'}


'''
@router.get("/customer/check")
async def check_customer(customer: customers.sCustomerCheck, acc_token: str = Depends(oauth2_scheme)):
    print("appCustomer: check started")
    token_validate(acc_token)
    print("appCustomer: token checked")
    return await customers_utl.do_cust_check(chkcustomer=customer)
'''